﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.ApplicationModel.DataTransfer;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Documents;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using 纸片人.win2dEffectControl;
using 纸片人.win2dEffectControl.DataClass;

// The Templated Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234235

namespace 纸片人.CalculateControls
{
    public  class Vector4Control : RangeControl
    {

        #region
        #region Vector4     
        /// <summary>
        /// 获取或设置Vector4的值
        /// </summary>  
        public Vector4 Vector4
        {
            get { return (Vector4)GetValue(Vector4Property); }
            set { SetValue(Vector4Property, value); }
        }

        /// <summary>
        /// 标识 Vector4 依赖属性。
        /// </summary>
        public static readonly DependencyProperty Vector4Property =
            DependencyProperty.Register("Vector4", typeof(Vector4), typeof(Vector4Control), new PropertyMetadata(new Vector4(), OnVector4Changed));

        private static void OnVector4Changed(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            Vector4Control target = obj as Vector4Control;
            Vector4 oldValue = (Vector4)args.OldValue;
            Vector4 newValue = (Vector4)args.NewValue;
            if (oldValue != newValue)
                target.OnVector4Changed(oldValue, newValue);
        }

        protected virtual void OnVector4Changed(Vector4 oldValue, Vector4 newValue)
        {
        }


        #endregion


        #region OneGenre     
        /// <summary>
        /// 获取或设置OneGenre的值
        /// </summary>  
        public Object OneGenre
        {
            get { return (Object)GetValue(OneGenreProperty); }
            set { SetValue(OneGenreProperty, value); }
        }

        /// <summary>
        /// 标识 OneGenre 依赖属性。
        /// </summary>
        public static readonly DependencyProperty OneGenreProperty =
            DependencyProperty.Register("OneGenre", typeof(Object), typeof(Vector4Control), new PropertyMetadata(null, OnOneGenreChanged));

        private static void OnOneGenreChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            Vector4Control target = obj as Vector4Control;
            Object oldValue = (Object)args.OldValue;
            Object newValue = (Object)args.NewValue;
            if (oldValue != newValue)
                target.OnOneGenreChanged(oldValue, newValue);
        }

        protected virtual void OnOneGenreChanged(Object oldValue, Object newValue)
        {
        }
        #endregion


        #region TwoGenre     
        /// <summary>
        /// 获取或设置TwoGenre的值
        /// </summary>  
        public Object TwoGenre
        {
            get { return (Object)GetValue(TwoGenreProperty); }
            set { SetValue(TwoGenreProperty, value); }
        }

        /// <summary>
        /// 标识 TwoGenre 依赖属性。
        /// </summary>
        public static readonly DependencyProperty TwoGenreProperty =
            DependencyProperty.Register("TwoGenre", typeof(Object), typeof(Vector4Control), new PropertyMetadata(null, OnTwoGenreChanged));

        private static void OnTwoGenreChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            Vector4Control target = obj as Vector4Control;
            Object oldValue = (Object)args.OldValue;
            Object newValue = (Object)args.NewValue;
            if (oldValue != newValue)
                target.OnTwoGenreChanged(oldValue, newValue);
        }

        protected virtual void OnTwoGenreChanged(Object oldValue, Object newValue)
        {
        }
        #endregion



        #region ThreeGenre     
        /// <summary>
        /// 获取或设置ThreeGenre的值
        /// </summary>  
        public Object ThreeGenre
        {
            get { return (Object)GetValue(ThreeGenreProperty); }
            set { SetValue(ThreeGenreProperty, value); }
        }

        /// <summary>
        /// 标识 ThreeGenre 依赖属性。
        /// </summary>
        public static readonly DependencyProperty ThreeGenreProperty =
            DependencyProperty.Register("ThreeGenre", typeof(Object), typeof(Vector4Control), new PropertyMetadata(null, OnThreeGenreChanged));

        private static void OnThreeGenreChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            Vector4Control target = obj as Vector4Control;
            Object oldValue = (Object)args.OldValue;
            Object newValue = (Object)args.NewValue;
            if (oldValue != newValue)
                target.OnThreeGenreChanged(oldValue, newValue);
        }

        protected virtual void OnThreeGenreChanged(Object oldValue, Object newValue)
        {
        }
        #endregion


        #region FourGenre     
        /// <summary>
        /// 获取或设置FourGenre的值
        /// </summary>  
        public Object FourGenre
        {
            get { return (Object)GetValue(FourGenreProperty); }
            set { SetValue(FourGenreProperty, value); }
        }

        /// <summary>
        /// 标识 FourGenre 依赖属性。
        /// </summary>
        public static readonly DependencyProperty FourGenreProperty =
            DependencyProperty.Register("FourGenre", typeof(Object), typeof(Vector4Control), new PropertyMetadata(null, OnFourGenreChanged));

        private static void OnFourGenreChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            Vector4Control target = obj as Vector4Control;
            Object oldValue = (Object)args.OldValue;
            Object newValue = (Object)args.NewValue;
            if (oldValue != newValue)
                target.OnFourGenreChanged(oldValue, newValue);
        }

        protected virtual void OnFourGenreChanged(Object oldValue, Object newValue)
        {
        }
        #endregion

        #endregion

        protected TextBoxSlider oneSlider;
        protected TextBoxSlider twoSlider;
        protected TextBoxSlider threeSlider;
        protected TextBoxSlider fourSlider;

        public Vector4Control()
        {
            this.DefaultStyleKey = typeof(Vector4Control);
        }

        protected override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            oneSlider = GetTemplateChildFormName<TextBoxSlider>(nameof(oneSlider));
            twoSlider = GetTemplateChildFormName<TextBoxSlider>(nameof(twoSlider));
            threeSlider = GetTemplateChildFormName<TextBoxSlider>(nameof(threeSlider));
            fourSlider = GetTemplateChildFormName<TextBoxSlider>(nameof(fourSlider));
        }

        protected override void OnexporterDrag(UIElement sender, DragStartingEventArgs args)
        {
            OnDragExportInvoke();
            OutputData od = new OutputData();
            od.Type = OutputType.Vector4;
            od.Exporter = this;
            od.ExportConnectElement = (ExporterDragControl)sender;
            od.ExporterInfo = nameof(Vector4);
            EffectOutputControl.outputData = od;
        }

        protected override void OnProviderEnter(object sednder, DragEventArgs e)
        {
            if (EffectOutputControl.outputData != null && EffectOutputControl.outputData.Type == OutputType.Vector4)
            {
                e.AcceptedOperation = DataPackageOperation.Copy;
                e.DragUIOverride.Caption = "拖进来了";
            }
        }

        protected override void OnProviderDrop(object sender, DragEventArgs e)
        {
            var outputData = EffectOutputControl.outputData;
            //判断是否形成了闭环了
            if (IsChartLoop(outputData.Exporter))
                return;
            if (outputData != null && outputData.Type == OutputType.Vector4)
            {
                var exporter = EffectOutputControl.outputData.Exporter;
                if (outputData.ExporterInfo is string str)
                {
                    exproterValueName = str;
                }
                else
                {
                    return;
                }
                (providerDrop as ProviderDropControl)?.SetConnectState();
                CreateConnect(ref oneConnectElement, providerDrop);
            }
        }

        protected override void Timer_Tick(object sender, object e)
        {
            if (oneConnectElement != null)
            {

                var exporter = oneConnectElement.Exporter;

                try
                {
                    var value = GetProvideValue(exporter, exproterValueName);
                    if (value is Vector4 v2)
                    {
                        if (v2 != Vector4)
                        {
                            Vector4 = v2;
                        }
                        if (oneSlider != null && oneSlider.Value != Vector4.X)
                        {
                            oneSlider.Value = Vector4.X;
                        }
                        if (twoSlider != null && twoSlider.Value != Vector4.Y)
                        {
                            twoSlider.Value = Vector4.Y;
                        }
                        if (threeSlider != null && threeSlider.Value != Vector4.Z)
                        {
                            threeSlider.Value = Vector4.Z;
                        }
                        if (fourSlider != null && fourSlider.Value != Vector4.W)
                        {
                            fourSlider.Value = Vector4.W;
                        }
                    }
                }
                catch
                {
                    exporter.Disconnect(oneConnectElement);
                    exporter = null;
                }

            }
            else
            {

                if (oneSlider.Value != Vector4.X || fourSlider.Value != Vector4.Y||threeSlider.Value!= Vector4.Z||fourSlider.Value!= Vector4.W)
                {
                    Vector4 = new Vector4((float)oneSlider.Value, (float)twoSlider.Value, (float)threeSlider.Value,(float)fourSlider.Value);
                }
            }
        }
    }
}
